package main;

import java.io.IOException;
import java.util.ArrayList;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import service.Allocator;
import service.Dumper;
import service.Fetcher;
import service.Indexer;
import util.FileLoader;
import crawler.CrawlerDocument;
import crawler.DocumentPool;

public class ThreadRunner {


	public static void main(String[] args) {
		
		PropertyConfigurator.configure("./conf/log4j.properties");
		
		final Logger logger = Logger.getLogger(ThreadRunner.class);
		
		logger.debug("test");
		
		String seedFile = "data/seed.json";
		
		DocumentPool urlPool = new DocumentPool();
		DocumentPool docPool = new DocumentPool();

		ArrayList<CrawlerDocument> seedlist = null;
		
		try {
			 seedlist = FileLoader.LoadSeed(seedFile);
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		Allocator allocator = new Allocator(urlPool);
		
		allocator.addSeedlist(seedlist);
		
		//Dumper.config("data/dump_data.txt", 10);

		
		Thread allocatorThread = new Thread(allocator);
		
		Thread fetcherThread = new Thread(new Fetcher(urlPool, docPool));
		
		Thread indexThread = new Thread(new Indexer(docPool));

		//Thread dumpThread = new Thread(Dumper.getInstance());
		
		allocatorThread.start();
		
		//dumpThread.start();
		
		/*
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
		
		fetcherThread.start();
		
		indexThread.start();
		
	}

}
